﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Configuration;

namespace SharpShare.Configuration {
    public sealed class ServicesSectionGroup : ConfigurationSectionGroup, IKeyedElementCollection<ServiceElement> {
        public new ReadOnlyCollection<IExtensionConfigurationSection<ServiceElement>> Sections {
            get {
                return ((ConfigurationSectionGroup)this).Sections.Cast<IExtensionConfigurationSection<ServiceElement>>().ToList().AsReadOnly();
            }
        }

        #region IKeyedElementCollection<ServiceElement> Members

        public ServiceElement this[string name] {
            get {
                foreach (IExtensionConfigurationSection<ServiceElement> section in this.Sections) {
                    ServiceElement element = section[name];
                    if (element != null) {
                        return element;
                    }
                }

                return null;
            }
        }

        public int Count {
            get { return this.Sections.Count; }
        }

        public ServiceElement CreateElement() {
            return null;
        }

        #endregion

        #region IKeyedElementCollection Members

        ConfigurationElement IKeyedElementCollection.this[string name] {
            get {
                return this[name];
            }
        }

        #endregion

        #region IEnumerable<StorageProviderElement> Members

        public IEnumerator<ServiceElement> GetEnumerator() {
            return this.Sections.SelectMany(s => s).Cast<ServiceElement>().GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }

        #endregion

        #region IKeyedElementCollection Members

        void IKeyedElementCollection.Add(ConfigurationElement element) {
            throw new NotImplementedException();
        }

        void IKeyedElementCollection.Remove(ConfigurationElement element) {
            throw new NotImplementedException();
        }

        ConfigurationElement IKeyedElementCollection.CreateElement() {
            throw new NotImplementedException();
        }

        #endregion
    }
}
